/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package start;

import java.rmi.Naming;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.swing.JOptionPane;

import model.Client;
import model.dao.DAOFactory;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import application.server.ReminderObserver;
import application.server.ServerImpl;
import application.server.interlending.Server;
import application.server.interlending.ServerHelper;
import application.server.jms.MyProducer;

/**
 * The Class for starting the Server.
 */
public class StartServer {

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {

		try {
			if (args.length != 4) {
				JOptionPane.showMessageDialog(null,
						"Startargumente fehlen! <ip> <port>", "Error",
						JOptionPane.ERROR_MESSAGE);
				System.exit(0);
			} else {
				System.setProperty("java.rmi.server.codebase", ServerImpl.class
						.getProtectionDomain().getCodeSource().getLocation()
						.toString());

				Naming.rebind("rmi://localhost:1099/LibMan", new ServerImpl());

				System.out.println("Server started");

				/*
				 * Properties prop = new Properties();
				 * prop.put(Context.INITIAL_CONTEXT_FACTORY,
				 * "com.sun.jndi.cosnaming.CNCtxFactory");
				 * prop.put(Context.PROVIDER_URL, "iiop://" + args[2] +":" +
				 * args[3]); ServerImpl serverRef = new ServerImpl(); Context
				 * initialNamingContext = new InitialContext(prop);
				 * initialNamingContext.rebind("LibMan", serverRef);
				 */
				/*
				 * application.server.interlending.ServerImpl serverRef2 = new
				 * application.server.interlending.ServerImpl(); Context
				 * initialNamingContext2 = new InitialContext(prop);
				 * initialNamingContext2.rebind("LibManInterlending",
				 * serverRef2);
				 */

				/*
				ORB orb = ORB.init(args, null);
				POA rootpoa = POAHelper.narrow(orb
						.resolve_initial_references("RootPOA"));
				rootpoa.the_POAManager().activate();
				application.server.interlending.ServerImpl cookieServant = new application.server.interlending.ServerImpl();
				org.omg.CORBA.Object ref = rootpoa
						.servant_to_reference(cookieServant);
				Server href = ServerHelper.narrow(ref);
				org.omg.CORBA.Object objRef = orb
						.resolve_initial_references("NameService");
				NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
				NameComponent cosName[] = ncRef.to_name("LibManObject");
				ncRef.rebind(cosName, href);*/

				// initialize the JMS
				MyProducer.getInstance();

				// thread to check the warnings for each client
				Thread t = new Thread(new ReminderObserver());
				t.start();

				System.out.println("Server ready and waiting ...");

				//orb.run();

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
